package com.party.core.service.mall.biz;

import com.google.common.collect.Lists;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.mall.*;
import com.party.core.service.mall.IMallCategoryService;
import com.party.core.service.mall.IMallGoodsService;
import com.party.core.service.mall.IMallShowSubjectService;
import com.party.core.service.mall.IMallStoreResourcesService;
import com.party.core.utils.MyBeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 店铺资源业务处理
 * Created by yifeng
 *
 * @date 2018/4/4 0004
 * @time 下午 16:30
 */
@Service
public class StoreResourceBizService {
    @Autowired
    private IMallStoreResourcesService mallStoreResourcesService;
    @Autowired
    private IMallCategoryService mallCategoryService;
    @Autowired
    private IMallShowSubjectService mallShowSubjectService;
    @Autowired
    private IMallGoodsService mallGoodsService;

    @Value("#{party['shop.url']}")
    private String shopUrl;

    Logger logger = LoggerFactory.getLogger(getClass());



    /**
     * 保存资源
     *
     * @param resources 资源
     * @param storeId   店铺
     */
    public void saveResourceBiz(List<MallStoreResources> resources, String storeId) {
        List<String> newResourceIds = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(resources)) {
            for (MallStoreResources resource : resources) {
                if (StringUtils.isEmpty(resource.getId())) {
                    resource.setStoreId(storeId);
                    mallStoreResourcesService.insert(resource);
                } else {
                    mallStoreResourcesService.update(resource);
                }
                newResourceIds.add(resource.getId());
            }
        }

        List<MallStoreResources> oldResources = mallStoreResourcesService.findByStoreId(storeId, null);
        List<String> oldResourceIds = LangUtils.transform(oldResources, input -> input.getId());
        oldResourceIds.removeAll(newResourceIds);
        for (String oldId : oldResourceIds) {
            mallStoreResourcesService.delete(oldId);
        }
    }

    /**
     * 获取资源
     *
     * @param storeId 店铺id
     */
    public MallStoreOutput getStoreResource(String storeId) {
        MallStoreOutput mallStore = new MallStoreOutput();
        // 轮播图
        List<MallStoreResources> carousel = mallStoreResourcesService.findByStoreId(storeId, MallStoreResources.MODEL_CAROUSEL);
        List<MallStoreResourcesOutput> carouselList = LangUtils.transform(carousel, input -> {
            MallStoreResourcesOutput output = new MallStoreResourcesOutput();
            try {
                MyBeanUtils.copyBeanNotNull2Bean(input, output);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String typeName = MallStoreResourcesType.getValue(input.getType());
            output.setTypeName(typeName);

            try {
                if (input.getType().equals(MallStoreResourcesType.SUBJECT.getCode())) {
                    MallShowSubject showSubject = mallShowSubjectService.get(input.getTargetId());
                    output.setResourceName(showSubject.getName());
                } else if (input.getType().equals(MallStoreResourcesType.CATEGORY.getCode())) {
                    MallCategory category = mallCategoryService.get(input.getTargetId());
                    output.setResourceName(category.getName());
                } else if (input.getType().equals(MallStoreResourcesType.LINK.getCode())) {
                    output.setResourceName(input.getUrl());
                } else if (input.getType().equals(MallStoreResourcesType.GOODS.getCode())) {
                    MallGoods mallGoods = mallGoodsService.get(input.getTargetId());
                    output.setResourceName(mallGoods.getName());
                }
            } catch (Exception e) {
                logger.error("获取资源异常", e);
            }
            return output;
        });
        // 分类导航
        List<MallStoreResources> category = mallStoreResourcesService.findByStoreId(storeId, MallStoreResources.MODEL_CATEGORY);
        List<MallStoreResourcesOutput> categoryList = LangUtils.transform(category, input -> {
            MallStoreResourcesOutput output = new MallStoreResourcesOutput();
            try {
                MyBeanUtils.copyBeanNotNull2Bean(input, output);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (MallStoreResourcesType.GOODS.getCode().equals(input.getType())) {
                MallGoods mallGoods = mallGoodsService.get(input.getTargetId());
                output.setChildName(mallGoods.getName());
                MallCategory mallCategory = mallCategoryService.get(input.getRemarks());
                if (mallCategory != null) {
                    output.setParentName(mallCategory.getName());
                }
            }
            return output;
        });
        List<MallStoreResources> deilyDeal = mallStoreResourcesService.findByStoreId(storeId, MallStoreResources.MODEL_DAILY_DEAL);
        List<MallStoreResources> friend = mallStoreResourcesService.findByStoreId(storeId, MallStoreResources.MODEL_FRIEND);
        List<MallStoreResources> brand = mallStoreResourcesService.findByStoreId(storeId, MallStoreResources.MODEL_BRAND);
        mallStore.setBrandList(brand);
        mallStore.setCarouselList(carouselList);
        mallStore.setCategoryList(categoryList);
        mallStore.setFriendList(friend);
        mallStore.setDailyDealList(deilyDeal);
        return mallStore;
    }

    /**
     * 动态拼接轮播图路径
     *
     * @param resources 资源
     * @return
     */
    public String appendLink(MallStoreResources resources) {
        String url = "";
        try {
            if (resources.getType().equals(MallStoreResourcesType.SUBJECT.getCode())) {
                MallShowSubject showSubject = mallShowSubjectService.get(resources.getTargetId());
                url = shopUrl + "subject?id=" + showSubject.getId();
            } else if (resources.getType().equals(MallStoreResourcesType.CATEGORY.getCode())) {
                MallCategory category = mallCategoryService.get(resources.getTargetId());
                url = shopUrl + "classify/" + category.getId();
            } else if (resources.getType().equals(MallStoreResourcesType.LINK.getCode())) {
                url = resources.getUrl();
            } else if (resources.getType().equals(MallStoreResourcesType.GOODS.getCode())) {
                MallGoods mallGoods = mallGoodsService.get(resources.getTargetId());
                url = shopUrl + "goods/" + mallGoods.getId();
            }
        } catch (Exception e) {
            logger.error("拼接链接异常", e);
        }
        return url;
    }
}
